/***************************************************************************
 * This program is free software; you can redistribute it and/or           *
 * modify it under the terms of the GNU General Public License             *
 * as published by the Free Software Foundation; either version 2          *
 * of the License, or (at your option) any later version.                  *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program; if not, write to the Free Software Foundation, *
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.         *
 *                                                                         *
 *   Copyright (C) 2007,2008 by Ivan Vucica                                *
 *   ivucica@gmail.com                                                     *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream> // REMOVE ME
#include "token.h"
#include "logosource.h"
#include "logoexpression.h"
Token Token::Evaluate(LogoContext& global, LogoContext& local) const {

	switch (type) {
		case TT_UNDEFINED:
			throw(std::string("TT_UNDEFINED type cannot be evaluated"));
		case TT_WORD:
			return Token(std::string("\"") + text);
		case TT_VARIABLE:
			if (local.variables.find(text)!=local.variables.end())
				return Token(local.variables[text]);
			else if (global.variables.find(text)!=global.variables.end())
				return Token(global.variables[text]);
			else
				throw(std::string("Variable ") + text + " undefined");

		case TT_NUMBER:
			return Token(text);
		case TT_EXPRESSION: {
			LogoExpression le(text.substr(1, text.size()-2));
			return le.Evaluate(global, local);
		}
		case TT_LIST:
			return *this;
        case TT_PROCEDURE: {
            //std::vector<Token> arguments;
            //for (int i = 0; i < global.procedures; <#increment#>) {
            //    <#statements#>
            //}
            return Token(std::string("[result of a function]"));
        }
		case TT_EOF:
		case TT_NL:
			return *this;
		default:
			return Token(std::string("[token::evaluate does not support element ") + Describe() + "]");
	}
	throw(std::string("Unexpected codeflow\n"));
}

